package com.fita.admin.web.controller.data;

import com.demo2do.core.entity.page.Page;
import com.demo2do.core.entity.result.HttpResult;
import com.demo2do.core.web.interceptor.MenuSetting;
import com.demo2do.core.web.utils.WebUtils;
import com.fita.admin.entity.search.data.SetSearch;
import com.fita.admin.service.data.SetService;
import com.fita.entity.set.Set;
import com.fita.entity.set.SetPayType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 套餐管理
 *
 * @author Ark
 *
 */
@Controller
@RequestMapping("/packages")
@MenuSetting("menu-data")
public class PackageController {
	
	private static final Logger logger = LoggerFactory.getLogger(PackageController.class);
	
	@Autowired
	private SetService setService;

	/**
	 * @return
	 */
	@RequestMapping(path = "/search", method = RequestMethod.GET)
    @MenuSetting("submenu-package-management")
	public ModelAndView search() {
		
		ModelAndView modelAndView = new ModelAndView("data/package/package-search");
		
		SetPayType[] payTypes = SetPayType.values();
		
		modelAndView.addObject("payTypes", payTypes);
		
		return modelAndView;
	}
	
	/**
	 * @param search
	 * @param page
	 * @param request
	 * @return
	 */
	@RequestMapping(path = "", method = RequestMethod.GET, params = "filter")
    @MenuSetting("submenu-package-management")
	public ModelAndView onSearch(SetSearch search, Page page, HttpServletRequest request) {
		
		ModelAndView modelAndView = new ModelAndView("data/package/package-list");
		
		int totals = setService.count(Set.class, search.getPreparedFilter(false));
		
		if (totals > 0) {
			
			List<Set> sets = setService.search(Set.class, search, page.with(totals));
			
			modelAndView.addObject("sets", sets);
			
			modelAndView.addObject("page", page);
			
            modelAndView.addObject("queryString", WebUtils.extractQueryString(request, "filter","page"));
		}
		return modelAndView;
	}	
	
    /**
     * @param page
     * @return
     */
    @RequestMapping(value = "" , method = RequestMethod.GET)
    @MenuSetting("submenu-package-management")
    public ModelAndView list(Page page) {
                      
        ModelAndView modelAndView = new ModelAndView("data/package/package-list");
        
        int totals = setService.count(Set.class);
        
        if (totals > 0) {
			
        	List<Set> sets = setService.listByPage(Set.class, page.with(totals));
        	
        	modelAndView.addObject("sets", sets);
        	
        	modelAndView.addObject("page", page);
		}
        return modelAndView;
    }
    
    /**
     * @param setId
     * @return
     */
    @RequestMapping(path = "/{setId}/open", method = RequestMethod.GET)
    public ModelAndView open(@PathVariable("setId") Long setId) {
    	
    	ModelAndView modelAndView = new ModelAndView("data/package/package-open");
    	
    	modelAndView.addObject("setId", setId);
    	
    	return modelAndView;
    }
    
    /**
     * @param setId
     * @return
     */
    @RequestMapping(path = "/{setId}/open", method = RequestMethod.PATCH, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody HttpResult onOpen(@PathVariable("setId") Long setId) {
    	
    	HttpResult httpResult = new HttpResult();
    	
    	try {
			setService.open(setId);
			
			httpResult.success().message("开启成功");
			
		} catch (Exception e) {
			
			httpResult.fail("500").message(e.getMessage());
			
			logger.error("#onOpen() open fail, exception is [{}]", e);
		}
    	return httpResult;
    }
    
    /**
     * @param setId
     * @return
     */
    @RequestMapping(path = "/{setId}/close", method = RequestMethod.GET)
    public ModelAndView close(@PathVariable("setId") Long setId) {
    	
    	ModelAndView modelAndView = new ModelAndView("data/package/package-close");
    	
    	modelAndView.addObject("setId", setId);
    	
    	return modelAndView;
    }
    
    /**
     * @param setId
     * @return
     */
    @RequestMapping(path = "/{setId}/close", method = RequestMethod.PATCH, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody HttpResult onClose(@PathVariable("setId") Long setId) {
    	
    	HttpResult httpResult = new HttpResult();
    	
    	try {
			setService.close(setId);
			
			httpResult.success().message("关闭成功");
			
		} catch (Exception e) {
			
			httpResult.fail("500").message(e.getMessage());
			
			logger.error("#onOpen() open fail, exception is [{}]", e);
		}
    	return httpResult;
    }
}
